package com.mianshi;

public class bilibili2 {
    public static void main(String[] args) {
        int[][] grid = {{0,0,1,0,0,0,0,1,0,0,0,0,0},
                        {0,0,0,0,0,0,0,1,1,1,0,0,0},
                        {0,1,1,0,1,0,0,0,0,0,0,0,0},
                        {0,1,0,0,1,1,0,0,1,0,1,0,0},
                        {0,1,0,0,1,1,0,0,1,1,1,0,0},
                        {0,0,0,0,0,0,0,0,0,0,1,0,0},
                        {0,0,0,0,0,0,0,1,1,1,0,0,0},
                        {0,0,0,0,0,0,0,1,1,0,0,0,0}};

        final Solution solution = new Solution();
        System.out.println(solution.maxAreaOfIsland(grid));
    }
}
 class Solution {
    int m, n;
     int ans = 0;
     int res = 0;
    public int maxAreaOfIsland(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(grid[i][j] == 1){
                    res = 0;
                    ans = Math.max(dfs(grid, i, j), ans);
                }
            }
        }
        return ans;
    }

    public int dfs(int[][] grid, int x, int y){
        if(x >= m || y >= n || x < 0 || y < 0 || grid[x][y] == 0){
            return 0;
        }
        res++;
        grid[x][y] = 0;
        int[] next_x = {1,-1,0,0};
        int[] next_y = {0,0,1,-1};
        for(int i = 0; i < 4; i++){
            dfs(grid, x+next_x[i], y+next_y[i]);
        }
        return res;
    }
}